<html>
<head><meta charset="utf-8"><title>Why can&#x27;t const generics use other generic parameters? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20const.20generics.20use.20other.20generic.20parameters.3F.html">Why can&#x27;t const generics use other generic parameters?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206145080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20const%20generics%20use%20other%20generic%20parameters%3F/near/206145080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20const.20generics.20use.20other.20generic.20parameters.3F.html#206145080">(Aug 06 2020 at 14:04)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">literal</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="nc">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">N</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<div class="codehilite"><pre><span></span><code>error[E0770]: the type of const parameters must not depend on other generic parameters
  --&gt; src/main.rs:12:30
   |
12 | const fn literal&lt;T, const N: T&gt;() -&gt; T {
   |                              ^ the type must not depend on the parameter `T`
</code></pre></div>


<p>Is this a hard limitation or a "we haven't done it yet" limitation?</p>



<a name="206152297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20const%20generics%20use%20other%20generic%20parameters%3F/near/206152297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kennytm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20const.20generics.20use.20other.20generic.20parameters.3F.html#206152297">(Aug 06 2020 at 14:57)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/74152">https://github.com/rust-lang/rust/issues/74152</a></p>
<blockquote>
<p>While we may want to support this in the future, this should not be part of the MVP and causes a lot of problems with our current approach. It causes both ICE ICE baby and breaks <code>foo&lt;const N: [u8; { 1+ 4 }]&gt;()</code> because <code>{ 1 + 4 }</code> contains <code>N</code> itself in its substitutions.</p>
</blockquote>



<a name="206152371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20const%20generics%20use%20other%20generic%20parameters%3F/near/206152371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20const.20generics.20use.20other.20generic.20parameters.3F.html#206152371">(Aug 06 2020 at 14:57)</a>:</h4>
<p>Sounds good, thanks!</p>



<a name="206152491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20const%20generics%20use%20other%20generic%20parameters%3F/near/206152491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20const.20generics.20use.20other.20generic.20parameters.3F.html#206152491">(Aug 06 2020 at 14:58)</a>:</h4>
<p>My case was something like</p>
<div class="codehilite"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[serde(default = </span><span class="s">&quot;literal::&lt;_, 42&gt;&quot;</span><span class="cp">]</span><span class="w"></span>
<span class="w">    </span><span class="n">bar</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="206171725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20const%20generics%20use%20other%20generic%20parameters%3F/near/206171725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20const.20generics.20use.20other.20generic.20parameters.3F.html#206171725">(Aug 06 2020 at 17:29)</a>:</h4>
<p>Sem-related: I wish serde just let you provide a literal directly. Serde's proc macro API could be a bit more user friendly.</p>



<a name="206188712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20const%20generics%20use%20other%20generic%20parameters%3F/near/206188712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20const.20generics.20use.20other.20generic.20parameters.3F.html#206188712">(Aug 06 2020 at 19:53)</a>:</h4>
<p>It certainly could be extended. I was surprised that <code>#[serde(default = "|| 42")]</code> didn't work.</p>



<a name="206188779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20const%20generics%20use%20other%20generic%20parameters%3F/near/206188779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20const.20generics.20use.20other.20generic.20parameters.3F.html#206188779">(Aug 06 2020 at 19:54)</a>:</h4>
<p>That's a fun trick I allow for in SNAFU for converting error types</p>



<a name="206188846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20const%20generics%20use%20other%20generic%20parameters%3F/near/206188846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20const.20generics.20use.20other.20generic.20parameters.3F.html#206188846">(Aug 06 2020 at 19:54)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="cp">#[snafu(from(Box::new))]</span><span class="w"></span>
<span class="c1">// or</span>
<span class="cp">#[snafu(from(|a| whatever_you_want))]</span><span class="w"></span>
</code></pre></div>



<a name="206188999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20can%27t%20const%20generics%20use%20other%20generic%20parameters%3F/near/206188999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20can&#x27;t.20const.20generics.20use.20other.20generic.20parameters.3F.html#206188999">(Aug 06 2020 at 19:56)</a>:</h4>
<p>But serde's method allows earlier error reporting</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>